# 对AGA (RISAdaptiveGeneticAlgorithm) 的消融实验(AblationStudy) 只有 Cross

import random
import numpy as np

from beam.util_psll import get_psll_by_phase


class AblationAGACross():
    __bit_num = 0  # 比特数
    __beam_num = 0  # 波束数

    def __init__(self, bit_num, beam_num, population_size=50, num_generations=100, num_parents=10, mutation_rate=0.1):
        # 初始化阵列相关
        self.__bit_num = bit_num
        self.__beam_num = beam_num
        # 初始化遗传算法相关
        self.population_size = population_size
        self.num_generations = num_generations
        self.num_parents = num_parents
        self.mutation_rate = mutation_rate
        self.population = None
        self.best_individual = None
        self.best_fitness = None
        self.best_fitness_history = []  # 保存每一代的最佳适应度值
        self.best_individual_history = []  # 保存每一代的最佳适应度值的个体
        # 新增自适应参数
        self.adaptive_params = {
            'pc_min': 0.6,  # 最小交叉概率[7](@ref)
            'pc_max': 0.9,  # 最大交叉概率[7](@ref)
            'pm_base': 0.05,  # 基础变异率[5](@ref)
            'pm_max': 0.2,  # 最大变异率[5](@ref)
            'diversity_threshold': 0.1  # 多样性阈值[6](@ref)
        }

    def initialize_population(self, phase_mix_init):
        """初始化种群"""
        self.population = [phase_mix_init] * self.population_size

    def fitness(self, phase):
        return get_psll_by_phase(phase, self.__bit_num, self.__beam_num)

    def selection(self):
        """选择操作"""
        fitness_scores = [self.fitness(individual) for individual in self.population]
        sorted_indices = np.argsort(fitness_scores)  # 从低到高排序
        selected_parents = [self.population[i] for i in sorted_indices[:self.num_parents]]
        # 获取选中的父代个体对应的适应度值
        selected_fitness_scores = [fitness_scores[i] for i in sorted_indices[:self.num_parents]]
        return selected_parents, selected_fitness_scores

    def adaptive_crossover(self, parents):
        """自适应多点交叉"""
        offspring = []
        fitness = np.array([self.fitness(p) for p in parents])
        std_dev = np.std(fitness)
        # 动态交叉率计算
        pc = self.adaptive_params['pc_max'] - \
             (self.adaptive_params['pc_max'] - self.adaptive_params['pc_min']) * \
             (std_dev / (std_dev + 1e-6))
        for _ in range(len(parents) // 2):
            p1, p2 = random.sample(parents, 2)
            if random.random() < pc:
                # 自适应多点交叉
                num_cuts = max(1, int(0.1 * len(p1)))
                cut_points = sorted(random.sample(range(1, len(p1)), num_cuts))
                child1 = np.concatenate([p1[:cut_points[0]],
                                         p2[cut_points[0]:cut_points[1]],
                                         p1[cut_points[1]:]])
                child2 = np.concatenate([p2[:cut_points[0]],
                                         p1[cut_points[0]:cut_points[1]],
                                         p2[cut_points[1]:]])
                offspring.extend([child1, child2])
            else:
                offspring.extend([p1, p2])
        return offspring

    def mutation(self, offspring):
        """变异操作"""
        for individual in offspring:
            if random.random() < self.mutation_rate:
                # 获取数组的数量和长度
                num_clusters, array_length = individual.shape
                # 生成一个与 individual 形状相同的随机数数组
                random_values = np.random.uniform(-180, 180, (num_clusters, array_length))
                # 决定哪些元素需要变异
                mask = np.random.rand(num_clusters, array_length) < self.mutation_rate
                # 应用变异
                individual[mask] = random_values[mask]
        return offspring

    def run(self, phase_mix_init, logger):
        logger.info("[AblationAGACross] population_size=%d, num_generations=%d, num_parents=%d, mutation_rate=%d"
                    % (self.population_size, self.num_generations, self.num_parents, self.mutation_rate))
        # """初始化返回值"""
        self.best_fitness = self.fitness(phase_mix_init)
        self.best_individual = phase_mix_init
        self.best_fitness_history = []
        self.best_individual_history = []
        # """运行遗传算法 -- 初始化阶段"""
        self.initialize_population(phase_mix_init)
        # """运行遗传算法 -- 搜索阶段"""
        for generation in range(self.num_generations):
            # 选择操作
            selected_parents, selected_fitness_scores = self.selection()
            # 交换操作
            offspring = self.adaptive_crossover(selected_parents)
            # 变异操作
            offspring = self.mutation(offspring)
            # 计算后代的适应度值
            offspring_fitness_scores = [self.fitness(individual) for individual in offspring]
            # 更新种群
            self.population = selected_parents + offspring
            # 合并父代和后代的适应度值
            all_fitness_scores = selected_fitness_scores + offspring_fitness_scores
            #
            # 找到当前代的最佳个体
            this_best_index = np.argmin(all_fitness_scores)  # 找到最佳适应度值的索引
            if this_best_index < len(selected_parents):
                this_best_individual = selected_parents[this_best_index]  # 最佳个体来自父代
            else:
                this_best_individual = offspring[this_best_index - len(selected_parents)]  # 最佳个体来自后代
            this_best_fitness = all_fitness_scores[this_best_index]  # 最佳适应度值
            #
            # 更新最佳适应度
            if this_best_fitness < self.best_fitness:
                self.best_fitness = this_best_fitness
                self.best_individual = this_best_individual
            # 记录最佳适应度曲线
            self.best_fitness_history.append(self.best_fitness)
            self.best_individual_history.append(self.best_individual)
            #
            # logger.info("generation=%d: self.best_fitness=%f, self.best_individual:%s"
            #             % (generation, self.best_fitness, self.best_individual))
            logger.info("generation=%d: self.best_fitness=%f" % (generation, self.best_fitness))
        return self.best_individual, self.best_fitness, self.best_fitness_history, self.best_individual_history